<template>
  <div class="app-container">
    <div style="margin-bottom: 20px;">
      <el-input v-model="listQuery.activityName" placeholder="请输入活动名称" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.activityStatus" placeholder="请选择状态" clearable class="filter-item" style="width: 120px">
        <el-option label="全部" value="" />
        <el-option label="未启用" value="0" />
        <el-option label="正在进行" value="1" />
        <el-option label="已结束" value="-1" />
      </el-select>
      <el-date-picker v-model="listQuery.activityStartTime" type="date" placeholder="活动开始日期" style="width: 160px;" />
      <el-date-picker v-model="listQuery.activityEndTime" type="date" placeholder="活动结束日期" style="width: 160px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-show="checkPermission('prizePoolAdd')" v-waves class="filter-item" type="primary" icon="el-icon-plus" style="float:right" @click="handleAdd">
        添加奖金池
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :element-loading-text="g.loadingStr"
      :header-cell-style="{background:g.mainColor}"
    >
      <el-table-column label="序号" align="center" width="100" fixed>
        <template slot-scope="{$index}">
          <span>{{ $index + 1 + ( listQuery.currentPage - 1 ) * listQuery.pageSize }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.activityName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参与机构" align="center">
        <template slot-scope="{row}">
          <span>{{ row.businessNames }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动开始时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.activityStartTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动失效时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.activityEndTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动金额/剩余金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.activityAmount }} / {{ row.surplusAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ row.activityStatus | prizePoolStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="280px">
        <template slot-scope="{row,$index}">
          <el-button v-show="row.activityStatus === 0" type="success" size="mini" @click="changeStatus(row,$index)">
            启用
          </el-button>
          <el-button v-show="row.activityStatus === 1" type="success" size="mini" @click="changeStatus(row,$index)">
            停用
          </el-button>
          <el-button v-show="row.activityStatus === 0 && checkPermission('prizePoolEdit')" type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-show="row.activityStatus === 0" type="danger" size="mini" @click="handleDel(row,$index)">
            删除
          </el-button>
          <p />
          <el-button type="primary" size="mini" @click="handleDe(row,$index)">
            数据统计
          </el-button>
          <el-button v-show="row.activityStatus !== 0" type="primary" size="mini" @click="handleGetLink(row,$index)">
            生成链接
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog
      title="生成二维码/网址链接"
      :visible.sync="dialogLinkVisible"
      width="30%"
    >
      <el-radio v-model="linkType" label="1">二维码</el-radio>
      <el-radio v-model="linkType" label="2">链接</el-radio>

      <p />
      <br>
      <div v-show="linkType === '1'" id="qrcode" />
      <el-input v-show="linkType === '2'" v-model="linkUrlStr" style="width: 100%;" class="filter-item" clearable />

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogLinkVisible = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

import QRcode from 'qrcodejs2'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { prizePoolList, prizePoolUpdate } from '@/api/prizePool'

export default {
  name: 'PrizePoolList',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        activityName: '',
        activityStatus: '',
        activityStartTime: null,
        activityEndTime: null
      },

      dialogLinkVisible: false,
      linkType: '1',
      linkUrlStr: '',
      qrcodeObj: null
    }
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      console.log(this.listQuery)
      this.listLoading = true

      const data = new Map()
      for (const key of Object.keys(this.listQuery)) {
        data[key] = this.listQuery[key] == null ? '' : this.listQuery[key]
      }
      data['activityStartTime'] = this.listQuery['activityStartTime'] === null ? '' : this.listQuery['activityStartTime'].getTime()
      data['activityEndTime'] = this.listQuery['activityEndTime'] === null ? '' : this.listQuery['activityEndTime'].getTime()

      prizePoolList(data).then(response => {
        console.log(response.data)
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    handleUpdate(row) {
      console.log(row)
      this.$router.push({ name: 'PrizePoolEdit', params: { id: row.id }})
    },
    handleGetLink(row, index) {
      this.dialogLinkVisible = true
      this.$nextTick(() => {
        this.linkUrlStr = 'https://app.jiabala.com/html/h5/bonusPool/index.html?couponId=' + row['id']
        this.getQrCode()
      })
    },
    getQrCode() {
      if (this.qrcodeObj === null) {
        this.qrcodeObj = new QRcode('qrcode', {
          width: 200,
          height: 200, // 高度
          text: this.linkUrlStr // 二维码内容
        })
        console.log(this.qrcodeObj)
      }
    },
    handleDel(row, index) {
      console.log(row)

      this.$confirm('是否确认删除该奖金池？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(JSON.stringify(this.multipleSelection))
        prizePoolUpdate({
          id: row['id'],
          status: -2
        }).then(response => {
          this.$message({ message: '操作成功', type: 'success' })
          this.getList()
        })
      })
    },
    handleAdd() {
      this.$router.push('PrizePoolAdd')
    },
    changeStatus(row, index) {
      console.log(row)
      console.log(index)
      prizePoolUpdate({
        id: row['id'],
        activityStatus: 1 - row['activityStatus']
      }).then(response => {
        this.$message({ message: '操作成功', type: 'success' })
        this.getList()
      })
    }
  }
}
</script>
